# -*- coding: utf-8  -*-
from django.db import models
from django.contrib.auth.models import User
class Author(models.Model):
    id = models.AutoField(primary_key=True)
    nick = models.CharField(maxlength=30, help_text="Forma przedstawienia")
    name = models.CharField(maxlength=75, help_text="Imie i/lub nazwisko")
    homepage = models.URLField(maxlength=75,default='http://')
    email = models.EmailField()
    about = models.TextField(blank=True,help_text="Coś o sobie")
    slug = models.SlugField(
        db_index=True,        
        prepopulate_from=('nick',),
        help_text="Tworzone automatycznie",
        )
    avatar = models.ImageField(upload_to="/home/soltys/django/mytemplate/img",
                               blank=True,
                               height_field ="120",
                               width_field = "75"
                               )
    def get_absolute_url(self):
        return "/blog/author/%s/" % (self.slug)
    def get_id_url(self):
        return "blog/author/%s/" % (self.id)
        
    def __str__(self):
        return self.nick
    class Admin:
        pass
    class Meta:
        verbose_name_plural = 'autorzy'

class Category(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(maxlength=100)
    
    slug = models.SlugField(
        db_index=True,        
        prepopulate_from=('name',),
        help_text="Tworzone automatycznie",
        )
    imagetag = models.ImageField(
        upload_to="/home/soltys/django/mytemplate/img",
        height_field ="100",
        width_field = "100",
        blank=True)
    
    def get_absolute_url(self):
        return "/blog/category/%s" % (self.name)
    
    def get_id_url(self):
        return "/blog/category/%s" % (self.id)
    
    def __str__(self):
        return self.name
    
    class Admin:
        pass

class Tag(models.Model):
    id = models.AutoField(primary_key=True,core=True)
    name = models.CharField(maxlength=20,core=True)
    slug = models.SlugField(
        prepopulate_from=('name',),
        help_text="Tworzone automatycznie",
        )
    class Admin:
        ordering = ['name']   
        pass
    
    def get_absolute_url(self):
        return "/blog/tag/%s/" % (self.slug)
    
    def get_id_url(self):
        return "/blog/tag/%s/" % (self.id)
    
    def __str__(self):
        return self.name
   
class Blog(models.Model):
    id = models.AutoField(primary_key=True)
    title  = models.CharField(maxlength=30)
    intro = models.TextField(
          blank=True,
          help_text="Opis/wstęp do postu nie jest obowiąskowe"
          )
    body = models.TextField()
    enable_comment = models.BooleanField(
        default=True,
        help_text="Możliwość komentowania (beta)"
        )
    
    last_mod = models.DateTimeField(auto_now=True)
    pub_date = models.DateTimeField(auto_now_add=True)
    slug = models.SlugField(
        unique_for_date='pub_date',
        prepopulate_from=('title',),
        help_text='Wypełniane automatycznie',
        )
    author = models.ForeignKey(Author)
    category = models.ForeignKey(Category)
    tag = models.ManyToManyField(
        Tag,
        filter_interface=models.HORIZONTAL,
        blank = True,
        )
    imagetag = models.ImageField(
        upload_to="/home/soltys/django/mytemplate/img",
        height_field ="100",
        width_field = "100",
        blank=True)
    
    mark = models.IntegerField(default='0')

    class Meta:
        ordering = ('-pub_date',)
        get_latest_by = 'pub_date'
        verbose_name_plural = 'Posty(blog)'

    class Admin:
        fields = (
            (None, {
            'fields': ('title', 'intro', 'body', 'author','category','tag',)
            }),
            ('Pola nie obowiązskowe', {
            'classes': 'collapse',
            'fields' : ('enable_comment', 'slug', 'mark','imagetag',)
            }),
            )
        
        list_display = ('title', 'author','slug','id','mark','pub_date')
        list_filter = ['pub_date']
        search_fields = ['tytul']
        search_fields = ['body']
        date_hierarchy = 'pub_date'
        pass
    
    def get_absolute_url(self):
        return "/blog/%s/%s/" %(self.pub_date.strftime("%Y/%h/%d").lower(), self.slug)
    
    def get_id_url(self):
        return "/blog/id/%s" %(self.id)
    
    def get_comment_count(self):
        from django.contrib.contenttypes.models import ContentType
        from django.contrib.comments.models import Comment
        ctype = ContentType.objects.get(name__exact='blog')
        num_comments = Comment.objects.filter(content_type=ctype.id, object_id=self.id).count()
        return num_comments
    def mark_pp(self):
        self.mark +=  1
        self.mark.save()
        return "/blog/"

    def __str__(self):
        return self.title
    
